package com.itheima.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.dto.EmpDto;
import com.itheima.domain.pojo.Emp;
import com.itheima.domain.pojo.EmpExpr;
import com.itheima.domain.vo.EmpVo;
import com.itheima.domain.vo.PageResult;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.service.EmpService;
import com.itheima.utils.AliyunOSSOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 上官徵羽
 * @version 1.0
 * @since 2025-08-15 20:44
 */
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @Override
    public PageResult<EmpVo> getEmpPage(EmpDto empDto) {
        PageHelper.startPage(empDto.getPage(), empDto.getPageSize());
        Page<EmpVo> page = empMapper.getEmpPage(empDto);
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    public List<Emp> getEmpList() {
        List<Emp> list = empMapper.getEmpList();
        return list;
    }

    @Override
    public void addEmp(Emp emp) {
        LocalDateTime now = LocalDateTime.now();
        // 设置员工属性
        emp.setPassword("123456");
        emp.setCreateTime(now);
        emp.setUpdateTime(now);
        empMapper.addEmp(emp);
        // 保存员工的工作经历
        List<EmpExpr> empExprList = emp.getEmpExpr();
        // 员工经历判空
        if (!ObjectUtils.isEmpty(empExprList)) {
            empExprList.stream()
                    .peek(empExpr -> empExpr.setEmpId(emp.getId()))
                    .toList();
            empExprMapper.addEmpExpr(empExprList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEmp(Integer[] ids) throws Exception {
        // 获取员工头像url
        List<String> empImage = empMapper.getEmpImageList(ids);
        // 删除员工头像
        if (!ObjectUtils.isEmpty(empImage)) {
            try {
                // 删除OSS的员工头像
                aliyunOSSOperator.deleteByUrls(empImage);
            } catch (Exception a) {
                throw new Exception("删除头像失败");
            }
        }
        // 删除员工经历
        empExprMapper.deleteEmpExpr(ids);
        // 批量删除员工
        empMapper.deleteEmp(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEmp(Emp emp) {
        // 数据回显
        Emp oldEmp = getEmpId(emp.getId());
        // 设置更新时间
        emp.setUpdateTime(LocalDateTime.now());
        // 修改员工头像 删除OSS中旧员工头像,添加新的
        // 判断新旧员工头像url是否一致,新头像会在执行修改sql语句时触发
        if (!oldEmp.getImage().equals(emp.getImage())){
            deleteEmpImage(oldEmp);
        }
        if (!ObjectUtils.isEmpty(emp.getEmpExpr())) {
            // 删除员工旧经历
            empExprMapper.deleteEmpExpr(new Integer[]{emp.getId()});
            // 添加员工新经历
            emp.getEmpExpr().stream()
                    .peek(empExpr -> empExpr.setEmpId(emp.getId()))
                    .toList();
            empExprMapper.addEmpExpr(emp.getEmpExpr());
        }
        // 两个判重,用户名 手机号
        // 1.username-用户名判重
        usernameRepeatJudgment(emp.getUsername(),oldEmp.getUsername());
        // 2.phone-手机号判重
        phoneRepeatJudgment(emp.getPhone(),oldEmp.getPhone());

        // 修改员工
        empMapper.updateEmp(emp);
    }

    @Override
    public Emp getEmpId(Integer id) {
        Emp emp = empMapper.getEmpId(id);
        return emp;
    }


    /** 手机号判重 */
    private void phoneRepeatJudgment(String phone, String phone1) {
        if (!phone.equals(phone1)) {
            Integer count = empMapper.getEmpPhoneCount(phone);
            if (count>0){
                throw new RuntimeException("手机号已存在");
            }
        }
    }

    /** 用户名判重 */
    private void usernameRepeatJudgment(String username, String username1) {
        if (username.equals(username1)) {
            Integer count = empMapper.getEmpUsernameCount(username);
            if (count>0){
                throw new RuntimeException("用户名已存在");
            }
        }
    }

    /** 删除OSS员工头像 */
    private void deleteEmpImage(Emp emp) {
        try {
            // 删除OSS中旧员工头像
            aliyunOSSOperator.deleteByUrls(List.of(emp.getImage()));
        } catch (Exception e) {
            throw new RuntimeException("删除头像失败");
        }
    }
}
